#pragma once
#include <Windows.h>
#include <string>
#include <list>
#include <map>
#include "comlib/comlib.h"
#include "../TextDecoder.h"
#include "../MonitorBase.h"

class CLocalMonitor : public RLocker, public MonitorBase {
    struct LocalLogCache {
        std::string mFilePath;
        LONGLONG mLastPos;
        LONGLONG mLastModified;
        LONGLONG mFileSize;
        std::string mLastCache;
        TextEncodeType mEncodeType;

        LocalLogCache() {
            mLastPos = 0;
            mLastModified = 0;
            mFileSize = 0;
            mEncodeType = em_text_unknown;
        }
    };

public:
    CLocalMonitor();
    virtual ~CLocalMonitor();
    bool AddPath(const std::string &path);
    bool DelPath(const std::string &path);

    virtual bool Init(CLogListener *pListener);
    virtual bool Run(const LogServDesc *desc);
    virtual bool Stop();
    virtual bool IsRunning();
    virtual std::list<std::string> GetPathSet() const;

private:
    bool IsNewLogFile(LPCSTR szFilePath) const;
    bool IsFileInCache(const std::string &filePath) const;
    LocalLogCache *GetFileCache(const std::string &filePath);
    void OnLogReceived(LocalLogCache *cache);
    static void FileNotify(const char *filePath, unsigned int mask, LPVOID pParam);

    TextEncodeType GetFileEncodeType(const std::string &filePath) const;
    std::string GetFileContent(const std::string &filePath, ULONGLONG lastPos, ULONGLONG llMaxCount);
private:
    bool mInit;
    std::map<std::string, DWORD> mPathSet;
    std::map<std::string, LocalLogCache *> mLogCache;
    const LogServDesc *mCfg;
    CLogListener *m_pListener;
};